You specify gradient fills and their attributes by including these atoms in a vector data stream:
For more information about gradients, see "Gradients for Path Fills" .
Listing 6 shows how to create a rectangle with a left-to-right linear gradient fill.
Listing 6 Adding a left-to-right linear gradient fill to a rectangle
ComponentInstance vectorCodec;
Handle streamH;
Handle pathH;
gxPoint points[4];
Boolean isOnCurve[4];
GradientColorRecord gradientColors[2];
ARGBColor white, black;
int i;
long value;
/* specify the gradient colors and where they start and end */
white.alpha = 255;
white.red = 255;
white.green = 255;
white.blue = 255;
black.alpha = 255;
black.red = 0;
black.green = 0;
black.blue = 0;
gradientColors[1].thisColor = white;
gradientColors[1].endingPercentage = ff(0);
gradientColors[2].thisColor = black;
gradientColors[2].endingPercentage = ff(1);
/* open the vector codec component */
OpenADefaultComponent (decompressorComponentType,
kVectorCodecType, &vectorCodec);
/* create a new vector data stream */
CurveCreateVectorStream (vectorCodec, streamH);
value=kLinearGradient;
/* add an atom to the vector data stream that specifies to
use linear gradient fills for subsequent paths */
CurveAddAtomToVectorStream (ci, kCurveGradientTypeAtom, sizeof(long),
&value, streamH);
/* add an atom to the vector data stream that specifies to
draw linear gradient fills from left to right for subsequent paths */
CurveAddAtomToVectorStream (ci, kCurveGradientAngleAtom, sizeof(Fixed),
ff(270), streamH);
/* add an atom to the vector data stream that specifies
gradient colors */
CurveAddAtomToVectorStream (ci, kCurveGradientRecordAtom,
sizeof(GradientColorRecord)*2,
gradientColors, streamH);
/* specify the points for the path and whether each one is
on the path */
points[0].x = ff(150);
points[0].y = ff(150);
isOnCurve[0] = TRUE;
points[1].x = ff(300);
points[1].y = ff(150);
isOnCurve[1] = TRUE;
points[2].x = ff(300);
points[2].y = ff(100);
isOnCurve[2] = TRUE;
points[3].x = ff(100);
points[3].y = ff(100);
isOnCurve[3] = TRUE;
/* create the path and add the points to it */
CurveNewPath (vectorCodec, &pathH);
for (i = 0; i <= 3; i++)
CurveInsertPointIntoPath (vectorCodec, &points[i], pathH,
1, i, isOnCurve[i]);
/* add the path to the vector data stream */
CurveAddPathAtomToVectorStream (vectorCodec, pathH, streamH);
/* mark the end of the vector data stream by adding a
kCurveEndAtom atom to the stream */
CurveAddZeroAtomToVectorStream (vectorCodec, streamH);
/* use the vector codec here to decompress and display the vector data */
/* dispose of stream and path handles when done */
DisposeHandle (streamH);
DisposeHandle (pathH);
Figure 67 shows what is drawn for this vector data stream.
Figure 67 A rectangle with a linear left-to-right gradient fill
| Previous | Chapter Contents | Chapter Top | Next |